﻿using System;
using System.Collections.Generic;
using System.Linq;

using PortalIntranet.DatabaseComponents;
using PortalIntranet.DatabaseComponents.WebParts.LinksGerais;
using PortalIntranet.ObjectComponents.WebParts.LinksGerais;
using PortalIntranet.DatabaseComponents.WebParts;

namespace PortalIntranet.BusinessComponents.WebParts.LinksGerais
{
	public class LinksGeraisBll
	{
		public static LinkGeral Obter(int id)
		{
			using (var contexto = new IntranetContexto())
			{
				return contexto.LinksGerais
					.Where(c => c.Id == id
						&& !c.Desativado
						&& !c.Excluido)
					.SingleOrDefault();
			}
		}

		public static List<LinkGeral> ListarLinksGerais(int webpart)
		{
			using (var contexto = new IntranetContexto())
			{
				return contexto.LinksGerais
					.Where(c => c.WebPartId == webpart
						&& !c.Desativado
						&& !c.Excluido)
					.OrderBy(o => o.Ordem)
					.ToList();
			}
		}

		public static WPLinkGeral ConverterWebPart(WebPart webpart)
		{
			List<LinkGeral> links = ListarLinksGerais(webpart.Id);
			WPLinkGeral wplink = new WPLinkGeral(webpart);

			foreach (var item in links)
			{
				wplink.Links.Add(new LinkGeralInfo(item));
			}

			return wplink;
		}
	}
}
